<?php

include('magic_quotes.php');
include('db_fns.php');
include('validate_fns.php');
include('photo_fns.php');
include('view_fns.php');

error_reporting(0);

$controller = 'photos';

$view = empty($_GET['view']) ?  'index' : $_GET['view'];

$this_weeks_theme = this_weeks_theme();

switch ($view) {

	case "index":
		$page_title = 'home';
		$layout = 'home';
		$themes = find_themes();
		break;

	case "archives":
		$page_title = "archives";
		$themes = find_themes();
		break;

	case "themes":
		$theme = find_theme($_GET['theme_id']);
		$photos = find_photos_by_theme($_GET['theme_id']);
		break;

	case "by_tag":
		$tag = $_GET['tag'];
		$photos = find_photos_by_tag($tag);
		break;

	case "cloud":
		$tags = find_tags();
		break;

	case "show":
		if($_GET['vote'])
		{
			photo_vote($_GET['id']);
		}
		$photo = find_photo($_GET['id']);
		$photos = find_photos_by_theme($photo['theme_id']);
		break;

	case "new":
		$page_title = 'Post Foto';
		break;

	case "delete":
		if($_GET['id'])
		{
			photo_delete($_GET['id']);
		}
		break;

	case "create":
		session_start();
		if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {

			if (is_jpeg($_FILES['photo']['type']['file'])
				and is_valid_file_size($_FILES['photo']['size']['file'])
				and is_uploaded_file($_FILES['photo']['tmp_name']['file'])
				and is_valid_width_height($_FILES['photo']['tmp_name']['file']))
			{
				if(is_fields_filled_out($_POST['photo']))
				{
					$_POST['photo']['theme_id'] = $this_weeks_theme['id'];

					$photo_id = create_photo($_POST['photo']);

					copy($_FILES['photo']['tmp_name']['file'], './photos/original/'.$photo_id.'.jpg');
					resize_photos($photo_id);
					$notice = "Successfully created photo";
				}
				else
				{
					$warning = "Click back and fill in all fields";
				}
			}
			else
			{
				$warning = "Error uploading file. Try again.
					             Please make sure your file is a JPEG and less than 2MB and width and 
					             height of the pic are greater than 250.";
			}

			unset($_SESSION['security_code']);
		}else {
				// Insert your code for showing an error message here
				echo '<div style="position: absolute; top:200px; left:400px; color:red; font-size: 1.6em;">Sorry, you have provided an invalid security code';
				echo '</div>';
		}

		break;

}

if(isset($layout))
{
	include($_SERVER['DOCUMENT_ROOT'].'/photos/views/layouts/'.$layout.'.php');
}
else
{
	include($_SERVER['DOCUMENT_ROOT'].'/photos/views/layouts/'.$controller.'.php');
}








?>
